import logging

import pytest

from resource.pom.gallery import Gallery
from utils.manager import MatchRules


@pytest.fixture(scope='function', autouse=True)
def func_setup_teardown(device):
    logging.info('------用例前置：预置视频资源------')
    app = Gallery
    device.mediatool_delete_all()
    device.hdc_file_send(app.video1, app.videos_dir)
    device.hdc_file_send(app.video2, app.videos_dir)
    device.hdc_file_send(app.video3, app.videos_dir)
    device.mediatool_send(app.videos_dir)
    yield
    logging.info('------用例后置：删除预置资源------')
    device.mediatool_delete_all()


class Test:
    app = Gallery

    @pytest.mark.parametrize('setup_teardown', [app.bundle_name], indirect=True)
    def test(self, setup_teardown, device):
        logging.info('步骤1.点击图库应用')
        device.start_ability(self.app.bundle_name, self.app.ability_name)
        logging.info('预期结果1.正常进入图库应用')
        device.assert_current_window(self.app.window_name)

        logging.info('步骤2.选择一个视频长按进入选项')
        device.refresh_layout()
        device.click_element(device.get_element_by_key('TabPhotos'))
        device.refresh_layout()
        device.long_click_element(device.get_element_by_key('Gesture_1'))
        logging.info('预期结果2.可以正常进入到视频选项页面')
        device.refresh_layout()
        device.assert_key_exist('ActionBarButtonCancel')
        device.assert_key_exist('SelectionTitleText')
        device.assert_key_exist('TimelinePage_GridItem0')
        device.assert_key_exist('SelectAllOfGroup0')
        device.assert_key_exist('Gesture_1')
        device.assert_key_exist('Previewer_1')
        device.assert_key_exist('Selector_1')
        # device.assert_key_exist('Gesture_2')
        # device.assert_key_exist('Previewer_2')
        # device.assert_key_exist('Selector_2')
        # device.assert_key_exist('Gesture_3')
        # device.assert_key_exist('Previewer_3')
        # device.assert_key_exist('Selector_3')
        device.assert_key_exist('ToolBarButtonSelectAll')
        device.assert_key_exist('ToolBarButtonDelete')
        device.assert_key_exist('ToolBarButtonMore')

        logging.info('步骤3.勾选一个或多个或全选或取消全选按钮')
        # 单选
        selector_1 = device.get_element_by_key('Selector_1')
        # selector_2 = device.get_element_by_key('Selector_2')
        selector_3 = device.get_element_by_key('Selector_3')
        before_click_1 = selector_1.get('checked')
        # before_click_2 = selector_2.get('checked')
        before_click_3 = selector_3.get('checked')
        device.click_element(selector_1)
        # device.click_element(selector_2)
        device.click_element(selector_3)
        device.refresh_layout()
        selector_1 = device.get_element_by_key('Selector_1')
        # selector_2 = device.get_element_by_key('Selector_2')
        selector_3 = device.get_element_by_key('Selector_3')
        after_click_1 = selector_1.get('checked')
        # after_click_2 = selector_2.get('checked')
        after_click_3 = selector_3.get('checked')
        # 全选
        device.click_element(device.get_element_by_key('ToolBarButtonSelectAll'))
        device.refresh_layout()
        select_all_check_boxs = device.get_elements_by_type('Checkbox')
        cancel_select = device.get_element_by_text('取消全选')
        # 取消全选
        device.click_element(device.get_element_by_key('ToolBarButtonSelectAll'))
        device.refresh_layout()
        cancel_select_all_check_boxs = device.get_elements_by_type('Checkbox')
        select_all = device.get_element_by_text('全选')
        logging.info('预期结果3.勾选一个或多个或全选或取消全选按钮有效')
        assert before_click_1 == 'true' and after_click_1 == 'false', '图片1的勾选状态异常'
        # assert before_click_2 == 'false' and after_click_2 == 'true', '图片2的勾选状态异常'
        assert before_click_3 == 'false' and after_click_3 == 'true', '图片3的勾选状态异常'
        for c in select_all_check_boxs:
            assert c.get('checked') == 'true', '图片未变成选中状态'
        assert cancel_select, '"全选"未变成"取消全选"'
        for c in cancel_select_all_check_boxs:
            assert c.get('checked') == 'false', '图片未变成选取消中状态'
        assert select_all, '"取消全选"未变成"全选"'

        logging.info('步骤4.点击删除按钮，选择删除')
        device.click_element(device.get_element_by_key('ToolBarButtonSelectAll'))
        device.click_element(device.get_element_by_key('ToolBarButtonDelete'))
        logging.info('预期结果4.显示“是否删除x个文件”的提示，点击删除成功')
        device.refresh_layout()
        device.assert_text_exist('是否删除', MatchRules.Contain)
        device.click_element(device.get_element_by_key('DeleteDialogConfirmButton'))
        logging.info('步骤5.查看视频结果')
        device.wait(3)
        device.refresh_layout()
        logging.info('预期结果5.视频库中已不存在被勾选删除的视频')
        # EmptyPhotoOrAlbumIco表示空白控件
        device.assert_key_exist('EmptyPhotoOrAlbumIco')
